--投资计划玩法

--#include "data\config\welfare\InvestPlanConfig.lua" once

--投资计划充值
function InvestPlanWithDrawYuanbaoHandler(sysarg)
   if not CheckConsumeCond( sysarg, InvestPlanCfg.Consumes, 1 ) then
      return
   end
   
   if not DoConsumeCond( sysarg, InvestPlanCfg.Consumes, 1, LogIds.InvestPlanLogId, Lang.LogTips.InvestPlan) then
      return
   end
   local sVar = Actor.getStaticVar(sysarg)
   if not sVar.investPlanDays then
      sVar.investPlanDays = 0
   end
   sVar.investPlanDays = sVar.investPlanDays + InvestPlanCfg.AwardDay
   local msg = string.format(Lang.ScriptTips.InvestPlan003, Actor.getName(sysarg))
   System.broadcastTipmsg(msg, ttScreenCenter + ttChatWindow)
   SendInvestPlanData(sysarg)
end

function IsInvestPlanOpen(sysarg)
   return Actor.checkActorLevel(sysarg, InvestPlanCfg.OpenLevel[2], InvestPlanCfg.OpenLevel[1])
end

--投资计划发送数据
function SendInvestPlanData(sysarg)
   if not IsInvestPlanOpen(sysarg) then return end
   local npack = DataPack.allocPacket(sysarg,LogicCmd.trSys.sysId,LogicCmd.trSys.sub.sGetInvestPlanData)  --申请一个数据包
   if (npack == nil) then
      --print("npack:nil")
      return
   end
   
   local sVar = Actor.getStaticVar(sysarg)
   if not sVar.investPlanDays then
      sVar.investPlanDays = 0
   end
   
   if not sVar.days then sVar.days = {} end


   --print("sVar.investPlanDays ="..sVar.investPlanDays)
   --local state = sVar.days.investPlanState or 0
   --print("sVar.days.investPlanState ="..state)
   DataPack.writeShort(npack, sVar.investPlanDays)  
   DataPack.writeByte(npack, sVar.days.investPlanState or 0)  


    DataPack.flush(npack)   --发送数据包
end

--投资计划领取奖励
function GetInvestPlanAward(sysarg)
   if not IsInvestPlanOpen(sysarg) then return end
   local sVar = Actor.getStaticVar(sysarg)
   if not sVar.investPlanDays then
      sVar.investPlanDays = 0
   end
   
   if not sVar.days then sVar.days = {} end

   if sVar.investPlanDays == 0 then
      Actor.sendTipmsg(sysarg, Lang.ScriptTips.InvestPlan001, ttFlyTip)
      return
   end
   if sVar.days.investPlanState and sVar.days.investPlanState == 1 then
      Actor.sendTipmsg(sysarg, Lang.ScriptTips.InvestPlan002, ttFlyTip)
      return
   end

   if not IsBagGridEnough(sysarg, InvestPlanCfg.Awards) then
      Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0015, ttFlyTip)
      return
   end

   sVar.investPlanDays = sVar.investPlanDays - 1
   sVar.days.investPlanState = 1

   local logId = LogIds.InvestPlanLogId
   local LogDesc = Lang.LogTips.InvestPlan
   GiveCommonAward(sysarg, InvestPlanCfg.Awards, logId, LogDesc)

   SendInvestPlanData(sysarg)
end


--协议处理
InvestPlanMessageHandle = function (sysarg, systemid, msgid, packet)
   if msgid == LogicCmd.trSys.sub.cGetInvestPlanData then    
      local opType = DataPack.readByte(packet)
      if opType == 0 then
         SendInvestPlanData(sysarg)
      elseif opType == 1 then
         InvestPlanWithDrawYuanbaoHandler(sysarg)
      end 
   elseif msgid == LogicCmd.trSys.sub.cGetInvestPlanAward then 
      GetInvestPlanAward(sysarg)
   end
end
